<?php

/**
 * @version     $Id: AdminsafeLogger.php 3 2010-10-08 17:49:35Z obalyuk87@gmail.com $
 * @package     AdminSafe
 * @subpackage  Components
 * @copyright   Copyright (C) 2010 Oleksandr Balyuk (www.boolcast.com)
 * @license     http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL v2
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
// No direct access
defined('_JEXEC') or die('Restricted access');
require_once JPATH_ADMINISTRATOR . DS . 'components' . DS . 'com_adminsafe' . DS . 'classes' . DS . 'AdminsafeObject.php';

/**
 * Description of AdminsafeLogger
 *
 * @author Oleksandr Balyuk <obalyuk@boolcast.com>
 */
class AdminsafeLogger extends AdminsafeObject {

    /**
     * Default Constructor
     */
    public function __construct() {
        parent::__construct();

        //
        $tablePath = $this->_componentPath . DS . 'tables';
        JTable::addIncludePath($tablePath);
    }

    /**
     * Logs user email
     *
     * @param string $email
     * @return bool True on success
     */
    public function recordUserLogin($email) {

        //
        $userEmail = $this->_db->Quote($email);
        $sql = "SELECT id FROM #__users WHERE email=$userEmail";
        $this->_db->setQuery($sql, 0, 1);
        $userId = (int) $this->_db->loadResult();

        //
        if ($userId) {
            //  Get Component Table
            $tbl = JTable::getInstance('accesslog', 'Table');
            //  Store data and return value
            return $tbl->save(array('user_id' => $userId));
        }


        //
        return false;
    }

    /**
     * Records Unsuccessful Admin Access
     *
     * @param string $keywordUsed
     * @return bool
     */
    public function recordUnsuccessfulAdminAccess($keywordUsed) {

        $tbl = JTable::getInstance('invalidkeylog', 'Table');
        return $tbl->save(array('key' => $keywordUsed));
    }

}